/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var app = new Vue({
    el: '#app',
    data: {
        selects:[],
        isFullCourt:1,
        isOpenExchange:1,
        schoolId:schoolId,
        schoolList:schoolMap,
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        that.getDicList("useScene");
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if(empty(token)){
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon:5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            },1200);
            return ;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        getDicList: function(keyTable) {
            var that = this;
            Get(that,_HOST+_API.common_dic_dicList+"?keyTable="+keyTable)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            that.selects = response.list;
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                });
        },
        selectIsFull: function () {
            var that = this;
            that.isFullCourt = $("#isFullCourt").val();
        },
        selectIsOpen: function () {
            var that = this;
            that.isOpenExchange = $("#isOpenExchange").val();
        },
        toSelectRelation: function() {
            var that = this;
            var selects = that.selects;
            var val = $("#useScene").val();
            var url = "";
            if (!empty(val)) {
                for (let i = 0; i < selects.length; i++) {
                    if (selects[i].keyS == val) {
                        url = selects[i].keyTableChineseName;
                    }
                }
            }
            if (!empty(url)) {
                var index = layer.open({
                    type: 2,
                    title: "选择场景关系",
                    content: url,
                    fix: true,
                    shadeClose: true,
                    shade: false,
                    area: ['70%', '80%'],
                    maxmin: true, //开启最大化最小化按钮
                    end: function () {
                        var relation = sessionStorageUtils.getJson(storageKey.sessionStorageKey.couponRelationTmp);
                        if (!empty(relation)) {
                            $("#useSceneRelation").val(relation.id);
                            if (!empty(relation.name)) {
                                $("#useSceneRelationName").val(relation.name);
                            }else if (!empty(relation.title)) {
                                $("#useSceneRelationName").val(relation.title);
                            }
                        }
                        sessionStorageUtils.del(storageKey.sessionStorageKey.couponRelationTmp);
                    }
                });
                layer.full(index);
            }
        },
        // 添加
        push:function () {
            var that = this;
            var ids = aGET.ids;
            if (empty(ids)) {
                return;
            }
            var idArr = ids.split(",");
            var arr = [];
            for (let i = 0; i < idArr.length; i++) {
                var obj = formToJson($("#Form").serialize());
                obj.compayId = companyId;
                obj.schoolId = schoolId;
                obj.couponId = idArr[i];
                arr.push(obj);
            }
            // console.log(param)
            Put(that,_HOST+_API.marketing_couponPush_batchPushCoupon,arr,true,"couponPush:batchPushCoupon")
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if(response){
                        if(response.success){
                            layer.msg(response.msg, {time: 1500, icon:1});
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                layer.closeAll();
                            }, 1500)
                        }else {
                            layer.msg(response.msg, {time: 1500, icon:5});
                        }
                    }
                })
        },
    },
});


$('#Form').data('bootstrapValidator', null);

